package com.atguigu.lease.common.exception;


import com.atguigu.lease.common.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e){
        System.err.println("发生通用异常："+e.getMessage());
        Result result = new Result();
        result.setData(e.getMessage());
        result.setMessage(e.getMessage());
        result.setCode(500);
        e.printStackTrace();
        return result;
    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeExceptionHandler(RuntimeException e){
        System.err.println("发生运行时异常："+e.getMessage());
        Result result = new Result();
        result.setData(e.getMessage());
        result.setMessage(e.getMessage());
        result.setCode(500);
        e.printStackTrace();
        return result;
    }

    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticExceptionHandler(ArithmeticException e){
        System.err.println("发生数学运算异常："+e.getMessage());
        Result result = new Result();
        result.setData(e.getMessage());
        result.setMessage(e.getMessage());
        result.setCode(500);
        e.printStackTrace();
        return result;
    }

}
